package invest.strategy.impl;

import invest.exception.BaseException;
import invest.pojo.Mail;
import invest.pojo.ToSendRecord;
import invest.pojo.XueqiuCookie;
import invest.utils.DateUtil;
import invest.utils.MailUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import invest.strategy.MailedRecord;
import invest.strategy.Strategy;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public abstract class AbstractStrategy implements Strategy, Job {
    //要发送的邮件
    protected Mail mail=new Mail();
    //要发送邮件内容的标的
    protected Set<ToSendRecord> toSendTarget;
    //相同标的的邮件间隔时间,防止之前发过邮件了，短时间重复发送
    protected Integer inpireMailDays=3;

    //默认cron时间
    public String cron="0 0/1 9-15 ? * 2-6";

    public synchronized Mail getMail() {
        return mail;
    }

    abstract protected void setMail();

    public Set<ToSendRecord> getToSendTarget() {
        return toSendTarget;
    }

    protected void setToSendTarget(ToSendRecord data) throws ParseException {
        Map<String, Date> mapRecord = MailedRecord.classMap.get(this.getClass());
        if (mapRecord.containsKey(data.getName())) {
            if (DateUtil.getDayDiffer(new Date(), mapRecord.get(data.getName())) > inpireMailDays) {
                mapRecord.put(data.getName(), new Date());
                this.toSendTarget.add(data);
            }
        } else {
            mapRecord.put(data.getName(), new Date());
            this.toSendTarget.add(data);
        }


    }

    protected void setInpireMailDays(Integer days) {
        this.inpireMailDays = days;
    }


    protected static final Float StringtoFloat(String s) {
        Float n = Float.valueOf(0);
        String pattern = "(\\-|\\+)?\\d+(\\.\\d+)?";
        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);
        // 现在创建 matcher 对象
        Matcher m = r.matcher(s);
        if (m.find( )) {
            n=Float.parseFloat( m.group(0) );

        } else {
            System.out.println("NO MATCH");
        }
        return n;
    }
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        doExecute();
    }
    private synchronized void doExecute() {
        try {
            if (setContext()) {
                Mail mail=getMail();
                MailUtil.sendMessage(mail.getSubject(), mail.getContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public synchronized boolean setContext() throws Exception {
        this.toSendTarget = new HashSet<>();
        if (analyzeStrategy()) {
            setMail();
            String toSendContent = "\n";
            for (ToSendRecord record : getToSendTarget()) {
                toSendContent = toSendContent+record.getName() + "   " + record.getMessage() + "   \n";
            }
            if (getToSendTarget()!=null&&getToSendTarget().size()>0) {
                if (mail.getContent() == null) {
                    throw new BaseException("没有设置邮件内容");
                } else {
                    String content = mail.getContent() + "\n" + toSendContent;
                    mail.setContent(content);
                    return true;
                }
            }
        }
        return false;
    }

}
